package com.web.wechat.global.util;

import com.blue.http.message.HttpRequest;
import com.blue.http.message.HttpSession;
import com.web.wechat.global.model.App;
import com.web.wechat.global.model.User;
import com.web.wechat.global.model.UserWechat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhengjin
 * @since 1.0 2018年03月27日
 */
@Component
public class HttpContext implements InitializingBean
{
	private static Logger logger = LoggerFactory.getLogger(HttpContext.class);

	private static final String USER_KEY = "user";
	private static final String APP_KEY = "app";
	private static final String LAST_URL_KEY = "last_url";
	private static final String USER_WECHAT = "user_wechat";
	private static final String VERIFY_KEY = "verify";

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	private HttpSession httpSession;

	public HttpContext()
	{
	}

	public void setUser(HttpRequest request, User user)
	{
		if (user == null)
		{
			httpSession.removeAttribute(request, USER_KEY);
		}
		else
		{
			httpSession.setAttribute(request, USER_KEY, user);
		}
	}

	public User getUser(HttpRequest request)
	{
		return (User) httpSession.getAttribute(request, USER_KEY);
	}

	public void setApp(HttpRequest request, App app)
	{
		if (app == null)
		{
			httpSession.removeAttribute(request, APP_KEY);
		}
		else
		{
			httpSession.setAttribute(request, APP_KEY, app);
		}
	}

	public App getApp(HttpRequest request)
	{
		return (App) httpSession.getAttribute(request, APP_KEY);
	}

	public void setVerify(HttpRequest request, String verify)
	{
		if (verify == null || verify.isEmpty())
		{
			httpSession.removeAttribute(request, VERIFY_KEY);
		}
		else
		{
			httpSession.setAttribute(request, VERIFY_KEY, verify);
		}
	}

	public String getVerify(HttpRequest request)
	{
		return (String) httpSession.getAttribute(request, VERIFY_KEY);
	}

	public void setLastUrl(HttpRequest request, String url, String queryString)
	{
		if (url == null || url.isEmpty())
		{
			httpSession.removeAttribute(request, LAST_URL_KEY);
			return;
		}

		StringBuilder sb = new StringBuilder(32);
		sb.append(url);
		if (queryString != null && !queryString.isEmpty())
			sb.append("?").append(queryString);

		httpSession.setAttribute(request, LAST_URL_KEY, sb.toString());
	}

	public void setLastUrl(HttpRequest request, String url)
	{
		if (url == null || url.isEmpty())
		{
			httpSession.removeAttribute(request, LAST_URL_KEY);
		}
		else
		{
			httpSession.setAttribute(request, LAST_URL_KEY, url);
		}
	}

	public String getLastUrl(HttpRequest request)
	{
		return (String) httpSession.getAttribute(request, LAST_URL_KEY);
	}

	public void setUserWechat(HttpRequest request, UserWechat uw)
	{
		if (uw == null)
		{
			httpSession.removeAttribute(request, USER_WECHAT);
		}
		else
		{
			httpSession.setAttribute(request, USER_WECHAT, uw);
		}
	}

	public UserWechat getUserWechat(HttpRequest request)
	{
		return (UserWechat) httpSession.getAttribute(request, USER_WECHAT);
	}

	@Override
	public void afterPropertiesSet() throws Exception
	{
		httpSession = new HttpSession(redisTemplate);
	}
}
